window.ROOT = "";
(function() {
	
	var serverData = {};
	var serverTable;
	jQuery(document).ready(function() {
		serverTable = jQuery('#dataTable').DataTable({
			"serverSide" : true,
			"ordering" : false,
			"ajax" : {
				"dataType" : "json",
				"type" : "get",
				"cache" : false,
				"url" : window.rootPath
						+ '/ui/visitorRewardProcessing/getpage',
				"data" : function(d) {
					return $.extend({}, d, serverData);
				}
			},
			columns : [
				{
					data : null,
					defaultContent : ""
				},
				{
					data : 'name'
				},
				{
					data : 'sex'
				},
				{
					data : 'contactType'
				},
				{
					data : 'email'
				},
				{
					data : 'memo'
				},
				{
					data : 'totalRewardValue'
				},
				{
					data : null,
					defaultContent : "",
					render : function(data,
							type, full,
							meta) {
						var operate = '<button type="button" class="btn btn-sm btn-info editBtn" title="处理"><i class="fa fa-gavel"></i></button>';

						return operate;

					}
				} ],
			"drawCallback" : function(oSettings) {
				var k = 0;
				var total = oSettings.aiDisplay.length
						+ oSettings._iDisplayStart;
	
				for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
					$(
							'td:eq(0)',
							oSettings.aoData[oSettings.aiDisplay[k]].nTr)
							.text(i + 1);
					k++;
				}
	
				serverTable.off('click', 'tr');
				serverTable.on('click','tr',function(event) {
					event = event || window.event;
					var target = event.target || event.scrElement;
					var data = serverTable.row(this).data();
					
					if (target.className.indexOf('fa-gavel') != -1 || target.className.indexOf('editBtn') != -1) {
						viewUser(data.id);
					}

					return;
				});
			}
		});
		
		var viewUser = function(data) {
			var url = window.rootPath + "/ui/visitorRewardProcessing/saveView/"+data;
			window.location.href = url;
		}
		// 查询
		jQuery('.searchBtn').on('click', function() {
			serverData.key = jQuery("#search").val();
			serverTable.draw();
		});
		// 撤销查询
		jQuery('.resetBtn').on('click', function() {
			jQuery('#search').val('');
			serverData.key = '';
			serverTable.draw();
		});

	});
	
})();